<?php

/**
 *  数字货币记录
 * @author xiaojy xiaojy@wanthings.com
 * @copyright 成都网信天成科技有限公司, 2016, http://www.wanthings.com
 */
class DigitalCashLogBehavior extends CActiveRecordBehavior
{

    /**
     * @param array $user 用户信息数组，电话，密码。
     * @return bool true false.
     *
     */
    public function getList($filter)
    {
        $result = array();
        $criteria = new CDbCriteria;
        $criteria->addColumnCondition(array('is_deleted' => 0));

        if (!empty($filter['sid'])) {
            $criteria->addCondition('sid=' . $filter['sid']);
        }

        if (!empty($filter['ids'])) {    //能够选择的ids
            $criteria->addInCondition('sid', $filter['ids']);
        }

        if (!empty($filter['min_time']) || !empty($filter['max_time'])) {
            if (!empty($filter['min_time']) && !empty($filter['max_time'])) {
                $criteria->addBetweenCondition('ctime', $filter['min_time'], $filter['max_time']);
            } elseif (!empty($filter['min_time']) && empty($filter['max_time'])) {
                $criteria->addCondition('ctime>' . $filter['min_time']);
            } else {
                $criteria->addCondition('ctime<=' . $filter['max_time']);
            }
        }

        if (isset($filter['style'])) {
            if ($filter['style'] == 1) {
                $criteria->order = 'id desc';
            } else {
                $criteria->order = 'id desc';
            }
        } else {
            $criteria->order = 'id desc';
        }

        if (!empty($filter['page']) || !empty($filter['page_size'])) {
            if (!empty($filter['page'])) {
                $page = $filter['page'];
            } else {
                $page = 1;
            }
            if (!empty($filter['page_size'])) {
                $page_size = $filter['page_size'];
            } else {
                $page_size = 20;
            }
            $count = $this->owner->model()->count($criteria);
            $pager = new CPagination($count);
            $pager->setItemCount($count);
            $pager->setCurrentPage($page - 1);
            $pager->setPageSize($page_size);
            $pager->validateCurrentPage = false;
            $pager->applyLimit($criteria);
            $result['pager'] = $pager;
        }
        $result['data'] = array();
        $query_result = $this->owner->findAll($criteria);
        if (!empty($query_result)) {
            foreach ($query_result as $key => $value) {
                $temp = $value->attributes;
                $result['data'][] = $temp;
            }
        }
        return $result;
    }


    private function findById($id)
    {
        $criteria = new CDbCriteria;
        $criteria->addColumnCondition(array('id' => $id, 'is_deleted' => 0));
        $re = $this->owner->find($criteria);
        return $re;
    }

    /**
     * @param $id 用户id
     * @return 用户信息数组.
     */
    public function getById($id)
    {
        $result = array();

        $re = $this->findById($id);
        if (!empty($re)) {
            $a = $re->attributes;
            $result['errno'] = 0;
            $result['errmsg'] = '';

            return $result;
        } else {
            $result['errno'] = 3126;
            $result['errmsg'] = '记录不存在';
            $result['result'] = '';
            return $result;
        }
    }

    //新增数据
    public function add($data)
    {
        $result = array();
        $result['errno'] = 0;
        $result['errmsg'] = 'success';
        $digital_cash = new DigitalCashLog;


        if (!empty($data['name'])) {
            $digital_cash->name = $data['name'];
        }
        if (!empty($data['short_name'])) {
            $digital_cash->short_name = $data['short_name'];
        }
        if (!empty($data['usd_price'])) {
            $digital_cash->usd_price = $data['usd_price'];
        }
        if (!empty($data['supply'])) {
            $digital_cash->supply = $data['supply'];
        }
        if (!empty($data['volume'])) {
            $digital_cash->volume = $data['volume'];
        }
        if (!empty($data['ranking'])) {
            $digital_cash->ranking = $data['ranking'];
        }
        if (!empty($data['rmb_price'])) {
            $digital_cash->rmb_price = $data['rmb_price'];
        }
        if (!empty($data['day_change'])) {
            $digital_cash->day_change = $data['day_change'];
        }
        if (!empty($data['market_cap'])) {
            $digital_cash->market_cap = $data['market_cap'];
        }
        if (!empty($data['sparkline'])) {
            $digital_cash->sparkline = $data['sparkline'];
        }
        $status = $digital_cash->save();
        if ($status) {
            $result['errno'] = 0;
            $result['errmsg'] = 'success';
        } else {
            $result['errno'] = 1;
            $result['errmsg'] = '保存失败';
        }
        return $result;
    }


    public function myTrim($str)
    {
        $search = array(" ", "　", "\n", "\r", "\t", "$", ",", "，", "%");
        $replace = array("", "", "", "", "");
        return str_replace($search, $replace, $str);
    }

    public function number($str)
    {
        return preg_replace('/[^\.0123456789]/s', '', $str);
    }


}

